<?php
class BannerController extends AppController {
    var $name = 'Banner';
    var $helpers = array('Html');
    var $components = array('Init', 'Param');
    var $uses = null;

    function beforeFilter() {
        if (!$this->Session->read('admin')) {
            $this->redirect('/users/login');
        }
        else {
            $this->Init->startup($this);
            $this->Admin = $this->Init->init();

            $this->set('dashboard',0);
            $this->set('content',0);
            $this->set('products',0);
            $this->set('component',1);
            $this->set('system',0);

            $this->set('title','Banner Management');
            $this->set('sub_title', $this->Param->getAction($this->params));
        }
    }

    function index(){
        $this->loadModel('Picture');
        $pictures = $this->Picture->find('all', array(
                                                                        'conditions' => array(
                                                                                'Picture.status' => 1,
                                                                                'Picture.kind' => 1
                                                                                ),
                                                                        'order' 	 => array('Picture.sort desc','Picture.date desc')
                                                                        ));
        $this->set('pictures',$pictures);
    }

    function edit($id = null){
        $this->loadModel('Picture');
        $this->helpers[] = 'Ck';
        $id = $this->Param->getID($this->params);

        if (isset($this->data)) {
                $this->data['Picture']['date'] = date('Y-m-d');
                $this->data['Picture']['kind'] = 1;
                if ($this->data['Picture']['image'] != "") {
                        App::import('Component', 'Image');
                        $image = new ImageComponent();
                        $_file = $image->upload_image_and_thumbnail($this->data,"image",661,370,"banner",true);

                        $this->data['Picture']['image_link'] = $_file;
                }

                if ($this->Picture->save($this->data)) {
                        $this->Session->setFlash('Done successfully!');
                        $this->redirect('/banner');
                }

        }
        else {
                if (isset($id) && ($id > 0)) {
                        $this->data = $this->Picture->find('first',array(
                                'conditions' => array('Picture.id' => $id)
                        ));
                }
        }

    }


    function del($id = null) {
        $this->loadModel('Picture');
        $id = $this->Param->getID($this->params);
        $this->Picture->id = $id;
        $this->Picture->delete($id);
        $this->Session->setFlash('Done successfully!');
        $this->redirect('/inside_banner');
    }
}
?>